home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / uu.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  4KB  |  176 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Implementation of the UUencode and UUdecode functions.
  5.  
  6. encode(in_file, out_file [,name, mode])
  7. decode(in_file [, out_file, mode])
  8. '''
  9. import binascii
  10. import os
  11. import sys
  12. __all__ = [
  13.     'Error',
  14.     'encode',
  15.     'decode']
  16.  
  17. class Error(Exception):
  18.     pass
  19.  
  20.  
  21. def encode(in_file, out_file, name = None, mode = None):
  22.     '''Uuencode file'''
  23.     if in_file == '-':
  24.         in_file = sys.stdin
  25.     elif isinstance(in_file, basestring):
  26.         if name is None:
  27.             name = os.path.basename(in_file)
  28.         
  29.         if mode is None:
  30.             
  31.             try:
  32.                 mode = os.stat(in_file).st_mode
  33.             except AttributeError:
  34.                 pass
  35.             except:
  36.                 None<EXCEPTION MATCH>AttributeError
  37.             
  38.  
  39.         None<EXCEPTION MATCH>AttributeError
  40.         in_file = open(in_file, 'rb')
  41.     
  42.     if out_file == '-':
  43.         out_file = sys.stdout
  44.     elif isinstance(out_file, basestring):
  45.         out_file = open(out_file, 'w')
  46.     
  47.     if name is None:
  48.         name = '-'
  49.     
  50.     if mode is None:
  51.         mode = 438
  52.     
  53.     out_file.write('begin %o %s\n' % (mode & 511, name))
  54.     data = in_file.read(45)
  55.     while len(data) > 0:
  56.         out_file.write(binascii.b2a_uu(data))
  57.         data = in_file.read(45)
  58.     out_file.write(' \nend\n')
  59.  
  60.  
  61. def decode(in_file, out_file = None, mode = None, quiet = 0):
  62.     '''Decode uuencoded file'''
  63.     if in_file == '-':
  64.         in_file = sys.stdin
  65.     elif isinstance(in_file, basestring):
  66.         in_file = open(in_file)
  67.     
  68.     while True:
  69.         hdr = in_file.readline()
  70.         if not hdr:
  71.             raise Error('No valid begin line found in input file')
  72.         
  73.         if not hdr.startswith('begin'):
  74.             continue
  75.         
  76.         hdrfields = hdr.split(' ', 2)
  77.         if len(hdrfields) == 3 and hdrfields[0] == 'begin':
  78.             
  79.             try:
  80.                 int(hdrfields[1], 8)
  81.             except ValueError:
  82.                 pass
  83.             except:
  84.                 None<EXCEPTION MATCH>ValueError
  85.             
  86.  
  87.         None<EXCEPTION MATCH>ValueError
  88.     if out_file is None:
  89.         out_file = hdrfields[2].rstrip()
  90.         if os.path.exists(out_file):
  91.             raise Error('Cannot overwrite existing file: %s' % out_file)
  92.         
  93.     
  94.     if mode is None:
  95.         mode = int(hdrfields[1], 8)
  96.     
  97.     opened = False
  98.     if out_file == '-':
  99.         out_file = sys.stdout
  100.     elif isinstance(out_file, basestring):
  101.         fp = open(out_file, 'wb')
  102.         
  103.         try:
  104.             os.path.chmod(out_file, mode)
  105.         except AttributeError:
  106.             pass
  107.  
  108.         out_file = fp
  109.         opened = True
  110.     
  111.     s = in_file.readline()
  112.     while s and s.strip() != 'end':
  113.         
  114.         try:
  115.             data = binascii.a2b_uu(s)
  116.         except binascii.Error:
  117.             v = None
  118.             nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) // 3
  119.             data = binascii.a2b_uu(s[:nbytes])
  120.             if not quiet:
  121.                 sys.stderr.write('Warning: %s\n' % v)
  122.             
  123.         except:
  124.             quiet
  125.  
  126.         out_file.write(data)
  127.         s = in_file.readline()
  128.     if not s:
  129.         raise Error('Truncated input file')
  130.     
  131.     if opened:
  132.         out_file.close()
  133.     
  134.  
  135.  
  136. def test():
  137.     '''uuencode/uudecode main program'''
  138.     import optparse as optparse
  139.     parser = optparse.OptionParser(usage = 'usage: %prog [-d] [-t] [input [output]]')
  140.     parser.add_option('-d', '--decode', dest = 'decode', help = 'Decode (instead of encode)?', default = False, action = 'store_true')
  141.     parser.add_option('-t', '--text', dest = 'text', help = 'data is text, encoded format unix-compatible text?', default = False, action = 'store_true')
  142.     (options, args) = parser.parse_args()
  143.     if len(args) > 2:
  144.         parser.error('incorrect number of arguments')
  145.         sys.exit(1)
  146.     
  147.     input = sys.stdin
  148.     output = sys.stdout
  149.     if len(args) > 0:
  150.         input = args[0]
  151.     
  152.     if len(args) > 1:
  153.         output = args[1]
  154.     
  155.     if options.decode:
  156.         if options.text:
  157.             if isinstance(output, basestring):
  158.                 output = open(output, 'w')
  159.             else:
  160.                 print sys.argv[0], ': cannot do -t to stdout'
  161.                 sys.exit(1)
  162.         
  163.         decode(input, output)
  164.     elif options.text:
  165.         if isinstance(input, basestring):
  166.             input = open(input, 'r')
  167.         else:
  168.             print sys.argv[0], ': cannot do -t from stdin'
  169.             sys.exit(1)
  170.     
  171.     encode(input, output)
  172.  
  173. if __name__ == '__main__':
  174.     test()
  175.  
  176.